﻿Imports DAO
Imports DTO
Imports System.Security.Cryptography

Public Class TaiKhoan_BUS

    Public Shared Function ktMatKhauTaiKhoan(ByVal tk As TaiKhoan_DTO) As Boolean
        If tk.matKhau.Length < 6 Then
            Throw New Exception("Mật khẩu phải lớn hơn 6 kí tự")
        End If
        Return True
    End Function

    Public Function LoadData() As List(Of TaiKhoan_DTO)
        Return TaiKhoan_DAO.LoadData
    End Function

    Public Shared Function CompareUserAndPass(ByVal user As String, ByVal pass As String, ByRef quyenhan As Integer) As Boolean
        Dim ds As List(Of TaiKhoan_DTO) = TaiKhoan_DAO.LoadData()

        If user.Length = 0 Or pass.Length = 0 Then
            Throw New ArgumentException("Tên đăng nhập và mật khẩu không được trống")
        End If

        Dim crypt = MD5.Create
        Dim passcrypt = CreateAndCompareMD5.GetMd5Hash(crypt, pass)

        For Each tk As TaiKhoan_DTO In ds
            If tk.tenTK = user And Trim(tk.matKhau) = passcrypt Then
                quyenhan = tk.maLoaiTK
                Return True
            End If
        Next

        Throw New ArgumentException("Tên đăng nhập hoặc mật khẩu không đúng")
        Return False
    End Function

    Public Shared Function kiemTraEmailTonTai(ByVal email As String) As Boolean
        Dim dt As DataTable = TaiKhoan_DAO.getEmail
        For Each r As DataRow In dt.Rows
            If Trim(r(0).ToString) = email Then
                Return True
            End If
        Next
        Return False
    End Function


    Public Shared Sub updateMatKhau(ByVal pass As String)
        Dim crypt = MD5.Create
        Dim passcrypt = CreateAndCompareMD5.GetMd5Hash(crypt, pass)
        TaiKhoan_DAO.updateMatKhau(passcrypt)
    End Sub


    Public Shared Function loadAllInfoOfNhanVien(ByVal tentk As String) As DataTable
        Return TaiKhoan_DAO.TaiKhoanGetAllInfo(tentk)
    End Function


    Public Shared Function loadInfo(ByVal tentk As String) As DataTable
        Return TaiKhoan_DAO.TaiKhoanGetInfo(tentk)
    End Function

End Class
